namespace Brjnk.GpxExplorer.WPF
{
    using System;
    using System.Windows.Data;

    public class DataLengthConverter : IValueConverter
    {
        private string[] units = {
		"B",
		"kB",
		"MB",
		"GB",
		"TB",
		"PB"
	};

        public object Convert(object value, System.Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            long val = (long)value;
            int unit = 0;
            if (val == 0)
            {
                unit = 0;
            }
            else
            {
                unit = (int)Math.Floor((Math.Log10(val) / Math.Log10(2)) / 10);
            }

            if (unit >= units.Length)
                unit = units.Length - 1;
            return string.Format("{2}{0:0.##} {1}", val / (Math.Pow(2, (unit * 10))), units[unit], parameter);
        }

        public object ConvertBack(object value, System.Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}